/*!
 * \file http_basedata.h
 *
 * \brief http 私有数据
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/01/28
 *
 */
#ifndef __HTTP_BASEDATA_H_
#define __HTTP_BASEDATA_H_

#include <sys/time.h>
#include <stdint.h>
#include <nghttp2/nghttp2.h>

#include "frreport_comm.h"
#include "frlist.h"
#include "frbyte_buffer.h"

#define HTTP_UNSET_DATA             0
#define HTTP_XML_DATA               1
#define HTTP_JSON_DATA              2
#define HTTP_TEXT_DATA              3
#define HTTP_FORM_DATA              4
#define HTTP_GZIP_DATA              5
#define HTTP_RMI_DATA               6
#define HTTP_YONGYOU_DATA           7
#define HTTP_JAVA_SERIALIZED        8
#define HTTP_HESSIAN_DATA           9
#define HTTP_OTHERS_DATA            10

typedef struct __st_comm_session
{
    unsigned long ul_seq;
    L4_transaction st_l4_trans;                         /* L4 信息统计 */

    Frreport_body_data *p_st_body_data_req;
    Frreport_body_data *p_st_body_data_resp;
    Frlist_header *p_lst_req_tmp;
    Frlist_header *p_lst_comm_tmp;

    char arr_tmp[16];

    char *p_uri;                                        /* title */
    char *p_code_string;
    char *p_err_msg;
    long l_response_code;
    int i_url_encoded;
    char arr_charset[32];

    int i_data_len;                                     /* 响应的数据长度 */
    int i_flag_need;                                    /* 响应信息是否为需要格式   0:未知   1:上报  2:不上报*/
    int i_flag_data_type;                               // 0:未知; 1:xml;2:json 3:text 4:multipart/form-data  5: gzip

    int i_flag_gzip;                                    /* 响应信息是否为经过gzip压缩 */
    int i_flag_deflate;                                 /* 响应信息是否为经过gzip压缩 */
    int i_flag_chunk;                                   /* 响应信息是否为chunk格式*/
    char *p_xml_data;

    int i_flag_multipart;
    char *p_boundary;

    int i_upgrade;                                      /* http/1.x --> http/2.0 */
    Frbyte_Packet *p_st_data;
    Frbyte_Packet *p_st_body_data;

    int i_flag_redetect;                                /* gzip 的数据 重新判断格式 */

    int i_chunk_left_len;

}Http_comm_session;

typedef struct __st_http1_session {
    Http_comm_session *p_st_comm_session;
    int i_flag_req_finish;                              /* 请求头解析结束标志 (1结束)*/
    int i_flag_req_report_end;                          /* 请求数据信息上报结束*/
    int i_flag_resp_finish;                             /* 响应头解析结束标志 (1结束)*/

    int i_flag_abandon;

    int i_start_pos;                                    /*memstr   起始查询位置*/

}Http1_session;

typedef struct __http2_session_data
{
    Http_comm_session *p_st_comm_session;
    uint32_t u32_stream_id;                                    /* stream id 31 bit */
    uint8_t u8_type;                                           /* 数据类型 */
    int i_unreported;                                          /* 是否有数据未上报 */
    char *p_title;
}Http2_session;

typedef struct __st_L7session_http_private
{
    int i_version;                                      /* 0:未知  1:http/1.x  2:http/2.0 */
    int i_flag_report;                                  /* 1:上报 2:不上报 */
    int i_frame_type;                                   /* 1:request   2:response   3:push promise */
    Http1_session *p_http1_sess;                        /* http1 私有数据 */

    Http2_session *p_http2_sess;                        /* http2 私有数据 */
    Frlist_header st_http2_session;                     /* http2 的session 数据存储 */

    uint32_t u32_current_resp_id;                              /* 当前的 resp ID */

    char *p_user_name;
    struct timeval st_login_timestamp;
    char arr_session_id[APM_UUID_SIZE];
    nghttp2_hd_inflater *p_inflater_ptr;
}L7session_http_private;

/* 4位小头节序转到本地字节序 */
#define pntohi(p)  ((unsigned int)*((const unsigned char *)(p + 0)) << 24 | \
                    (unsigned int)*((const unsigned char *)(p + 1)) << 16 | \
                    (unsigned int)*((const unsigned char *)(p + 2)) << 8 | \
                    (unsigned int)*((const unsigned char *)(p + 3)) << 0)

#endif //__HTTP_BASEDATA_H_
